***************
* Loading data*
***************

*** Load the data

*********************
* Preparing the data*
*********************

* Variable: dv_all
* Description: Whether the country is using low district magnitude or high electoral threshold

gen dv_all=0
replace dv_all=1 if dv_legal==1
replace dv_all=1 if dv_dm==1

* Variable: adopt_all

* Description: Whether the parliament adopted (ie. use it at t0, while didn't use it at t-1) low district magnitude or high electoral threshold by year

by country (year), sort: gen byte adopt_all = sum(dv_all == 1) == 1  & sum(dv_all[_n - 1] == 1) == 0  
by country (year), sort: gen lag_dv_all = dv_all[_n-1]
replace adopt_all=1 if lag_dv_all==0 & dv_all==1

replace adopt_all=0 if country=="Belgium" & year==1946
replace adopt_all=0 if country=="Denmark" & year==1947
replace adopt_all=0 if country=="Finland" & year==1948
replace adopt_all=0 if country=="Luxembourg" & year==1945
replace adopt_all=0 if country=="Netherlands" & year==1946
replace adopt_all=0 if country=="Sweden" & year==1948
replace adopt_all=0 if country=="Switzerland" & year==1948

* Variable: number_all
* Descritpion: Number of times low district magnitudes or high electoral thresholds have been used previously since 1945 (missing if low district magnitudes and high electoral threshold currently in use)
 
gen number_all=.
replace number_all=1 if year>=1970 & year<=1994 & country=="Austria"
replace number_all=1 if year>=1985 & year<=2003 & country=="Belgium"
replace number_all=1 if year>=1991 & country=="Bulgaria"
replace number_all=0 if year<=1995 & country=="Croatia"
replace number_all=1 if year>=1996 & country=="Cyprus"
replace number_all=1 if year>=1971 & year<=1997 & country=="Denmark"
replace number_all=0 if country=="Finland"
replace number_all=0 if country=="Italy" & year<=1994
replace number_all=1 if country=="Italy" & year>=2006
replace number_all=0 if year<=1995 & country=="Latvia"
replace number_all=0 if country=="Luxembourg"
replace number_all=0 if country=="Netherlands"
replace number_all=1 if year>=1973 & country=="Norway"
replace number_all=0 if year<=1993 & country=="Poland"
replace number_all=0 if country=="Portugal"
replace number_all=0 if year<=2000 & country=="Romania"
replace number_all=0 if country=="Slovenia"
replace number_all=0 if country=="Sweden"
replace number_all=0 if country=="Switzerland"

* Variables: group_ling, group_demo, group_bound_[country]
* Description: Groups of linguistic, historical and geographical peers to which the democracy belongs

gen group_ling=.
replace group_ling=1 if country=="Italy"
replace group_ling=1 if country=="Portugal"
replace group_ling=1 if country=="Romania"
replace group_ling=1 if country=="Spain"
replace group_ling=2 if country=="Germany" 
replace group_ling=2 if country=="Austria" 
replace group_ling=2 if country=="Switzerland"
replace group_ling=2 if country=="Norway"
replace group_ling=2 if country=="Sweden"
replace group_ling=2 if country=="Iceland"
replace group_ling=2 if country=="Netherlands"
replace group_ling=2 if country=="Belgium"
replace group_ling=2 if country=="Luxembourg"
replace group_ling=2 if country=="Denmark"
replace group_ling=3 if country=="Czech Republic"
replace group_ling=3 if country=="Slovakia"
replace group_ling=3 if country=="Poland"
replace group_ling=3 if country=="Slovenia"
replace group_ling=3 if country=="Croatia"
replace group_ling=3 if country=="Bulgaria"
replace group_ling=3 if country=="Latvia"
replace group_ling=3 if country=="Lithuania" 
replace group_ling=4 if country=="Greece"
replace group_ling=4 if country=="Cyprus"
replace group_ling=5 if country=="Hungary"
replace group_ling=5 if country=="Finland"
replace group_ling=5 if country=="Estonia"

gen group_demo=.
replace group_demo=1 if country=="Czech Republic"
replace group_demo=1 if country=="Slovakia"
replace group_demo=1 if country=="Poland"
replace group_demo=1 if country=="Slovenia"
replace group_demo=1 if country=="Hungary"
replace group_demo=1 if country=="Croatia"
replace group_demo=1 if country=="Bulgaria"
replace group_demo=1 if country=="Latvia"
replace group_demo=1 if country=="Lithuania"
replace group_demo=1 if country=="Estonia"
replace group_demo=1 if country=="Romania"
replace group_demo=2 if country=="Cyprus"
replace group_demo=2 if country=="Greece"
replace group_demo=2 if country=="Portugal"
replace group_demo=2 if country=="Spain"
replace group_demo=3 if country=="Austria"
replace group_demo=3 if country=="Switzerland"
replace group_demo=3 if country=="Norway"
replace group_demo=3 if country=="Sweden"
replace group_demo=3 if country=="Iceland"
replace group_demo=3 if country=="Netherlands"
replace group_demo=3 if country=="Belgium"
replace group_demo=3 if country=="Luxembourg"
replace group_demo=3 if country=="Denmark"
replace group_demo=3 if country=="Finland"
replace group_demo=3 if country=="Germany"
replace group_demo=3 if country=="Italy"

gen group_bound_aus=.
replace group_bound_aus=1 if country=="Austria"
replace group_bound_aus=1 if country=="Czech Republic"
replace group_bound_aus=1 if country=="Germany"
replace group_bound_aus=1 if country=="Hungary"
replace group_bound_aus=1 if country=="Italy"
replace group_bound_aus=1 if country=="Slovakia"
replace group_bound_aus=1 if country=="Slovenia"
replace group_bound_aus=1 if country=="Switzerland"

gen group_bound_bel=.
replace group_bound_bel=1 if country=="Belgium"
replace group_bound_bel=1 if country=="Germany"
replace group_bound_bel=1 if country=="Netherlands"
replace group_bound_bel=1 if country=="Luxembourg"

gen group_bound_bul=.
replace group_bound_bul=1 if country=="Bulgaria"
replace group_bound_bul=1 if country=="Greece"
replace group_bound_bul=1 if country=="Romania"

gen group_bound_cro=.
replace group_bound_cro=1 if country=="Croatia"
replace group_bound_cro=1 if country=="Hungary"
replace group_bound_cro=1 if country=="Slovenia"
replace group_bound_cro=1 if country=="Italy"

gen group_bound_cyp=.
replace group_bound_cyp=1 if country=="Cyprus"
replace group_bound_cyp=1 if country=="Greece"

gen group_bound_cze=.
replace group_bound_cze=1 if country=="Czech Republic"
replace group_bound_cze=1 if country=="Slovakia"
replace group_bound_cze=1 if country=="Poland"
replace group_bound_cze=1 if country=="Hungary"
replace group_bound_cze=1 if country=="Germany"

gen group_bound_den=.
replace group_bound_den=1 if country=="Denmark"
replace group_bound_den=1 if country=="Germany"
replace group_bound_den=1 if country=="Norway"
replace group_bound_den=1 if country=="Sweden"
replace group_bound_den=1 if country=="Poland"

gen group_bound_est=.
replace group_bound_est=1 if country=="Estonia"
replace group_bound_est=1 if country=="Finland"
replace group_bound_est=1 if country=="Latvia"
replace group_bound_est=1 if country=="Sweden"

gen group_bound_fin=.
replace group_bound_fin=1 if country=="Finland"
replace group_bound_fin=1 if country=="Sweden"
replace group_bound_fin=1 if country=="Norway"
replace group_bound_fin=1 if country=="Estonia"

gen group_bound_ger=.
replace group_bound_ger=1 if country=="Germany"
replace group_bound_ger=1 if country=="Netherlands"
replace group_bound_ger=1 if country=="Belgium"
replace group_bound_ger=1 if country=="Luxembourg"
replace group_bound_ger=1 if country=="Czech Republic"
replace group_bound_ger=1 if country=="Austria"
replace group_bound_ger=1 if country=="Denmark"
replace group_bound_ger=1 if country=="Poland"
replace group_bound_ger=1 if country=="Switzerland"
replace group_bound_ger=1 if country=="Sweden"

gen group_bound_gre=.
replace group_bound_gre=1 if country=="Greece"
replace group_bound_gre=1 if country=="Bulgaria"
replace group_bound_gre=1 if country=="Cyprus"
replace group_bound_gre=1 if country=="Italy"

gen group_bound_hun=.
replace group_bound_hun=1 if country=="Hungary"
replace group_bound_hun=1 if country=="Romania"
replace group_bound_hun=1 if country=="Slovakia"
replace group_bound_hun=1 if country=="Austria"
replace group_bound_hun=1 if country=="Croatia"
replace group_bound_hun=1 if country=="Slovenia"

gen group_bound_ice=.

gen group_bound_ita=.
replace group_bound_ita=1 if country=="Italy"
replace group_bound_ita=1 if country=="Switzerland"
replace group_bound_ita=1 if country=="Slovenia"
replace group_bound_ita=1 if country=="Austria"
replace group_bound_ita=1 if country=="Greece"
replace group_bound_ita=1 if country=="Spain"
replace group_bound_ita=1 if country=="Croatia"

gen group_bound_lat=.
replace group_bound_lat=1 if country=="Latvia"
replace group_bound_lat=1 if country=="Estonia"
replace group_bound_lat=1 if country=="Lithuania"
replace group_bound_lat=1 if country=="Sweden"

gen group_bound_lit=.
replace group_bound_lit=1 if country=="Lithuania"
replace group_bound_lit=1 if country=="Latvia"
replace group_bound_lit=1 if country=="Poland"
replace group_bound_lit=1 if country=="Sweden"

gen group_bound_lux=.
replace group_bound_lux=1 if country=="Luxembourg"
replace group_bound_lux=1 if country=="Belgium"
replace group_bound_lux=1 if country=="Germany"

gen group_bound_net=.
replace group_bound_net=1 if country=="Netherlands"
replace group_bound_net=1 if country=="Germany"
replace group_bound_net=1 if country=="Belgium"

gen group_bound_nor=.
replace group_bound_nor=1 if country=="Norway"
replace group_bound_nor=1 if country=="Sweden"
replace group_bound_nor=1 if country=="Iceland"
replace group_bound_nor=1 if country=="Finland"

gen group_bound_pol=.
replace group_bound_pol=1 if country=="Poland"
replace group_bound_pol=1 if country=="Germany"
replace group_bound_pol=1 if country=="Czech Republic"
replace group_bound_pol=1 if country=="Slovakia"
replace group_bound_pol=1 if country=="Lithuania"
replace group_bound_pol=1 if country=="Sweden"
replace group_bound_pol=1 if country=="Denmark"

gen group_bound_por=.
replace group_bound_por=1 if country=="Portugal"
replace group_bound_por=1 if country=="Spain"

gen group_bound_rom=.
replace group_bound_rom=1 if country=="Romania"
replace group_bound_rom=1 if country=="Bulgaria"
replace group_bound_rom=1 if country=="Hungary"

gen group_bound_slo=.
replace group_bound_slo=1 if country=="Slovakia"
replace group_bound_slo=1 if country=="Czech Republic"
replace group_bound_slo=1 if country=="Austria"
replace group_bound_slo=1 if country=="Hungary"
replace group_bound_slo=1 if country=="Poland"

gen group_bound_slove=.
replace group_bound_slove=1 if country=="Slovenia"
replace group_bound_slove=1 if country=="Croatia"
replace group_bound_slove=1 if country=="Italy"
replace group_bound_slove=1 if country=="Austria"
replace group_bound_slove=1 if country=="Hungary"

gen group_bound_spa=.
replace group_bound_spa=1 if country=="Spain"
replace group_bound_spa=1 if country=="Portugal"
replace group_bound_spa=1 if country=="Italy"

gen group_bound_swe=.
replace group_bound_swe=1 if country=="Sweden"
replace group_bound_swe=1 if country=="Norway"
replace group_bound_swe=1 if country=="Denmark"
replace group_bound_swe=1 if country=="Finland"
replace group_bound_swe=1 if country=="Germany"
replace group_bound_swe=1 if country=="Latvia"
replace group_bound_swe=1 if country=="Lithuania"
replace group_bound_swe=1 if country=="Poland"
replace group_bound_swe=1 if country=="Estonia"

gen group_bound_swi=.
replace group_bound_swi=1 if country=="Switzerland"
replace group_bound_swi=1 if country=="Germany"
replace group_bound_swi=1 if country=="Austria"
replace group_bound_swi=1 if country=="Italy"

* Variables: n_peers_ling, n_peers_demo, n_peers_bound
* Description: Number of democracies in each of the linguistic, historical and geographical group of peers by year

gen n=1

bysort year group_ling: egen n_peers_ling = total(n)
replace n_peers_ling=n_peers_ling-1

bysort year group_demo: egen n_peers_demo = total(n)
replace n_peers_demo=n_peers_demo-1

bysort year group_bound_aus: egen n_peers_bound_aus = total(n) if group_bound_aus==1
replace n_peers_bound_aus = n_peers_bound_aus-1
bysort year group_bound_bel: egen n_peers_bound_bel = total(n) if group_bound_bel==1
replace n_peers_bound_bel = n_peers_bound_bel-1
bysort year group_bound_bul: egen n_peers_bound_bul = total(n) if group_bound_bul==1
replace n_peers_bound_bul = n_peers_bound_bul-1
bysort year group_bound_cro: egen n_peers_bound_cro = total(n) if group_bound_cro==1
replace n_peers_bound_cro = n_peers_bound_cro-1
bysort year group_bound_cyp: egen n_peers_bound_cyp = total(n) if group_bound_cyp==1
replace n_peers_bound_cyp = n_peers_bound_cyp-1
bysort year group_bound_cze: egen n_peers_bound_cze = total(n) if group_bound_cze==1
replace n_peers_bound_cze = n_peers_bound_cze-1
bysort year group_bound_den: egen n_peers_bound_den = total(n) if group_bound_den==1
replace n_peers_bound_den = n_peers_bound_den-1
bysort year group_bound_est: egen n_peers_bound_est = total(n) if group_bound_est==1
replace n_peers_bound_est = n_peers_bound_est-1
bysort year group_bound_fin: egen n_peers_bound_fin = total(n) if group_bound_fin==1
replace n_peers_bound_fin = n_peers_bound_fin-1
bysort year group_bound_ger: egen n_peers_bound_ger = total(n) if group_bound_ger==1
replace n_peers_bound_ger = n_peers_bound_ger-1
bysort year group_bound_gre: egen n_peers_bound_gre = total(n) if group_bound_gre==1
replace n_peers_bound_gre = n_peers_bound_gre-1
bysort year group_bound_hun: egen n_peers_bound_hun = total(n) if group_bound_hun==1
replace n_peers_bound_hun = n_peers_bound_hun-1
bysort year group_bound_ice: egen n_peers_bound_ice = total(n) if group_bound_ice==1
replace n_peers_bound_ice = n_peers_bound_ice-1
bysort year group_bound_ita: egen n_peers_bound_ita = total(n) if group_bound_ita==1
replace n_peers_bound_ita = n_peers_bound_ita-1
bysort year group_bound_lat: egen n_peers_bound_lat = total(n) if group_bound_lat==1
replace n_peers_bound_lat = n_peers_bound_lat-1
bysort year group_bound_lit: egen n_peers_bound_lit = total(n) if group_bound_lit==1
replace n_peers_bound_lit = n_peers_bound_lit-1
bysort year group_bound_lux: egen n_peers_bound_lux = total(n) if group_bound_lux==1
replace n_peers_bound_lux = n_peers_bound_lux-1
bysort year group_bound_net: egen n_peers_bound_net = total(n) if group_bound_net==1
replace n_peers_bound_net = n_peers_bound_net-1
bysort year group_bound_nor: egen n_peers_bound_nor = total(n) if group_bound_nor==1
replace n_peers_bound_nor = n_peers_bound_nor-1
bysort year group_bound_pol: egen n_peers_bound_pol = total(n) if group_bound_pol==1
replace n_peers_bound_pol = n_peers_bound_pol-1
bysort year group_bound_por: egen n_peers_bound_por = total(n) if group_bound_por==1
replace n_peers_bound_por = n_peers_bound_por-1
bysort year group_bound_rom: egen n_peers_bound_rom = total(n) if group_bound_rom==1
replace n_peers_bound_rom = n_peers_bound_rom-1
bysort year group_bound_slo: egen n_peers_bound_slo = total(n) if group_bound_slo==1
replace n_peers_bound_slo = n_peers_bound_slo-1
bysort year group_bound_slove: egen n_peers_bound_slove = total(n) if group_bound_slove==1
replace n_peers_bound_slove = n_peers_bound_slove-1
bysort year group_bound_spa: egen n_peers_bound_spa = total(n) if group_bound_spa==1
replace n_peers_bound_spa = n_peers_bound_spa-1
bysort year group_bound_swe: egen n_peers_bound_swe = total(n) if group_bound_swe==1
replace n_peers_bound_swe = n_peers_bound_swe-1
bysort year group_bound_swi: egen n_peers_bound_swi = total(n) if group_bound_swi==1
replace n_peers_bound_swi = n_peers_bound_swi

gen n_peers_bound=.
replace n_peers_bound=n_peers_bound_aus if country=="Austria"
replace n_peers_bound=n_peers_bound_bel if country=="Belgium"
replace n_peers_bound=n_peers_bound_bul if country=="Bulgaria"
replace n_peers_bound=n_peers_bound_cro if country=="Croatia"
replace n_peers_bound=n_peers_bound_cyp if country=="Cyprus"
replace n_peers_bound=n_peers_bound_cze if country=="Czech Republic"
replace n_peers_bound=n_peers_bound_den if country=="Denmark"
replace n_peers_bound=n_peers_bound_est if country=="Estonia"
replace n_peers_bound=n_peers_bound_fin if country=="Finland"
replace n_peers_bound=n_peers_bound_ger if country=="Germany"
replace n_peers_bound=n_peers_bound_gre if country=="Greece"
replace n_peers_bound=n_peers_bound_hun if country=="Hungary"
replace n_peers_bound=n_peers_bound_ice if country=="Iceland"
replace n_peers_bound=n_peers_bound_ita if country=="Italy"
replace n_peers_bound=n_peers_bound_lat if country=="Latvia"
replace n_peers_bound=n_peers_bound_lit if country=="Lithuania"
replace n_peers_bound=n_peers_bound_lux if country=="Luxembourg"
replace n_peers_bound=n_peers_bound_net if country=="Netherlands"
replace n_peers_bound=n_peers_bound_nor if country=="Norway"
replace n_peers_bound=n_peers_bound_pol if country=="Poland"
replace n_peers_bound=n_peers_bound_por if country=="Portugal"
replace n_peers_bound=n_peers_bound_rom if country=="Romania"
replace n_peers_bound=n_peers_bound_slo if country=="Slovakia"
replace n_peers_bound=n_peers_bound_slove if country=="Slovenia"
replace n_peers_bound=n_peers_bound_spa if country=="Spain"
replace n_peers_bound=n_peers_bound_swe if country=="Sweden"
replace n_peers_bound=n_peers_bound_swi if country=="Switzerland"

* Variables: all_ling, all_demo, all_bound
* Description: Proportion of linguistic, historical and geographical peers that have adopted low district magnitude or high electoral threshold by year

bysort year group_ling: egen all_ling=total(adopt_all)
bysort year group_demo: egen all_demo=total(adopt_all)
bysort year group_bound_aus: egen all_bound_aus= total(adopt_all)
bysort year group_bound_bel: egen all_bound_bel= total(adopt_all)
bysort year group_bound_bul: egen all_bound_bul= total(adopt_all)
bysort year group_bound_cro: egen all_bound_cro= total(adopt_all)
bysort year group_bound_cyp: egen all_bound_cyp= total(adopt_all)
bysort year group_bound_cze: egen all_bound_cze= total(adopt_all)
bysort year group_bound_den: egen all_bound_den= total(adopt_all)
bysort year group_bound_est: egen all_bound_est= total(adopt_all)
bysort year group_bound_fin: egen all_bound_fin= total(adopt_all)
bysort year group_bound_ger: egen all_bound_ger= total(adopt_all)
bysort year group_bound_gre: egen all_bound_gre= total(adopt_all)
bysort year group_bound_hun: egen all_bound_hun= total(adopt_all)
bysort year group_bound_ice: egen all_bound_ice= total(adopt_all)
bysort year group_bound_ita: egen all_bound_ita= total(adopt_all)
bysort year group_bound_lat: egen all_bound_lat= total(adopt_all)
bysort year group_bound_lit: egen all_bound_lit= total(adopt_all)
bysort year group_bound_lux: egen all_bound_lux= total(adopt_all)
bysort year group_bound_net: egen all_bound_net= total(adopt_all)
bysort year group_bound_nor: egen all_bound_nor= total(adopt_all)
bysort year group_bound_pol: egen all_bound_pol= total(adopt_all)
bysort year group_bound_por: egen all_bound_por= total(adopt_all)
bysort year group_bound_rom: egen all_bound_rom= total(adopt_all)
bysort year group_bound_slo: egen all_bound_slo= total(adopt_all)
bysort year group_bound_slove: egen all_bound_slove= total(adopt_all)
bysort year group_bound_spa: egen all_bound_spa= total(adopt_all)
bysort year group_bound_swe: egen all_bound_swe= total(adopt_all)
bysort year group_bound_swi: egen all_bound_swi= total(adopt_all)

gen all_bound=.
replace all_bound=all_bound_aus if country=="Austria"
replace all_bound=all_bound_bel if country=="Belgium"
replace all_bound=all_bound_bul if country=="Bulgaria"
replace all_bound=all_bound_cro if country=="Croatia"
replace all_bound=all_bound_cyp if country=="Cyprus"
replace all_bound=all_bound_cze if country=="Czech Republic"
replace all_bound=all_bound_den if country=="Denmark"
replace all_bound=all_bound_est if country=="Estonia"
replace all_bound=all_bound_fin if country=="Finland"
replace all_bound=all_bound_ger if country=="Germany"
replace all_bound=all_bound_gre if country=="Greece"
replace all_bound=all_bound_hun if country=="Hungary"
replace all_bound=all_bound_ice if country=="Iceland"
replace all_bound=all_bound_ita if country=="Italy"
replace all_bound=all_bound_lat if country=="Latvia"
replace all_bound=all_bound_lit if country=="Lithuania"
replace all_bound=all_bound_lux if country=="Luxembourg"
replace all_bound=all_bound_net if country=="Netherlands"
replace all_bound=all_bound_nor if country=="Norway"
replace all_bound=all_bound_pol if country=="Poland"
replace all_bound=all_bound_por if country=="Portugal"
replace all_bound=all_bound_rom if country=="Romania"
replace all_bound=all_bound_slo if country=="Slovakia"
replace all_bound=all_bound_slove if country=="Slovenia"
replace all_bound=all_bound_spa if country=="Spain"
replace all_bound=all_bound_swe if country=="Sweden"
replace all_bound=all_bound_swi if country=="Switzerland"

replace all_ling=all_ling-1 if adopt_all==1
replace all_demo=all_demo-1 if adopt_all==1
replace all_bound=all_bound-1 if adopt_all==1

replace all_ling=all_ling/n_peers_ling
replace all_demo=all_demo/n_peers_demo
replace all_bound=all_bound/n_peers_bound

* Variables: imit[time-span]_all_ling, imit[time-span]_all_demo, imit[time-span]_all_bound
* Description: Cumulative sum of the proportion of linguistic, historical and geographical peers that have have adopted low district magnitude or high elecotral thresholds within [time-span]

* 2 years
by country (year), sort: gen imit2_all_ling = sum(all_ling)-sum(all_ling[_n-2])
by country (year), sort: gen imit2_all_demo = sum(all_demo)-sum(all_demo[_n-2])
by country (year), sort: gen imit2_all_bound = sum(all_bound)-sum(all_bound[_n-2])

* Variables: lag_enpv, lag_enp_seat, lag_majority, lag_semi_pres, lag_federal, lag_constitution_pr, lag_parties_gov
* Description: Lagging covariates

sort country year 
by country: gen lag_enpv = enpv[_n-1]

sort country year 
by country: gen lag_enp_seat = enp_seat[_n-1]

* Analyses
***********


logit dv_all imit2_all_bound lag_enpv lag_enp_seat if election_year==1 & number_all!=., cluster(country)
logit dv_all imit2_all_ling lag_enpv lag_enp_seat if election_year==1 & number_all!=., cluster(country)
logit dv_all imit2_all_demo lag_enpv lag_enp_seat if election_year==1 & number_all!=., cluster(country)





